package com.zhiwen.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Auther: wuzhiwen
 * @Date: 2018/12/28 10:04
 * @Description:
 */
public class AIOClient {
    private AsynchronousSocketChannel client = null;

    public AIOClient(String ip,int port) throws ExecutionException, InterruptedException, IOException {
        client = AsynchronousSocketChannel.open();
        Future<Void> connect = client.connect(new InetSocketAddress(ip, port));
        System.out.println(connect.get());
    }

    public void write(byte b){
        ByteBuffer byteBuffer = ByteBuffer.allocate(32);
        byteBuffer.put(b) ;
        byteBuffer.flip();
        client.write(byteBuffer);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
        AIOClient aioClient = new AIOClient("127.0.0.1", 8010);
        aioClient.write((byte) 27);
    }

}

